/*action create by pionner code builder*/

package com.pioneer.app.sms.mobile;

import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.DocumentFactory;
import org.dom4j.Element;
import org.hibernate.Query;
import org.hibernate.Transaction;

import config.BaseHibernateDAO;

public class UpSmsDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(UpSms.class);
	/**根据主键查找对象*/
	public UpSms findById(java.lang.Integer id){
		log.debug("getting UpSms instance with id: " + id);
		try {
			UpSms instance =(UpSms)getSession().get("com.pioneer.app.sms.mobile.UpSms",id);
			return instance;
		}catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	/**删除对象*/
	public void delete(UpSms persistentInstance){
		log.debug("deletting UpSms persistentInstance  ");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		}catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	/**添加对象*/
	public void add(UpSms persistentInstance){
		log.debug("deletting UpSms persistentInstance  ");
		try {
			getSession().save(persistentInstance);
			log.debug("add successful");
		}catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	/**更新对象*/
	public void update(UpSms persistentInstance){
		log.debug("deletting UpSms persistentInstance  ");
		try {
			getSession().update(persistentInstance);
			log.debug("update successful");
		}catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	/**检索对象*/
	public List findByCondition(String condition){
		log.debug("finding by condition "+condition);
		try {

			String queryString =null;
			if(null!=condition){
				queryString = "from UpSms where "+condition;
			}else{queryString = "from UpSms";
			}

			Query queryObject = getSession().createQuery(queryString);
			queryObject.setMaxResults(500);
			return queryObject.list();
		}catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	/**删除*/
	public void deleteObjs(java.lang.Integer[]ids){
		log.debug("deleting by objects ");
		try {
			if(null!=ids){
				UpSms obj=null;
				java.lang.Integer id=null;
				for(int i=0;i<ids.length;i++){
					id=ids[i];
					obj=this.findById(id);
					this.delete(obj);
				}
			}
		}catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	/**提供弹出窗体检索内容*/
	public Document findSelWinDoc( String condition){
		Document doc=null;
		try {
			List ls=this.findByCondition(condition);
			doc=DocumentFactory.getInstance().createDocument();
			Element root=doc.addElement("NODES");
			Iterator it=ls.iterator();
			Element elt=null;
			UpSms obj=null;
			while(it.hasNext()){
				obj=(UpSms)it.next();
				elt=root.addElement("NODE");
				elt.addAttribute("id", ""+obj.getId().intValue());
				elt.addAttribute("name", obj.getTxt());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return doc;
	}

	public static void main(String[]args){
		UpSmsDAO updao=new UpSmsDAO();
		UpSms vo=new UpSms();
		vo.setTxt("test1111");
		vo.setDestAddr("sdfjsdkfj");
		vo.setServiceCode("001");
		Transaction tx=updao.getSession().beginTransaction();
		updao.add(vo);
		tx.commit();
	}
}
